﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.DAO.Admin;
using DHQGHN.DAL.Models.Admin;

namespace DHQGHN.DAL.BO.Admin
{
    public class AdmUnitBO : BaseBO
    {
        #region Field list from database
        public const string FIELD_ADM_UNIT_ID = "ADM_UNIT_ID";
        public const string FIELD_CODE = "CODE";
        public const string FIELD_P_CODE = "P_CODE";
        public const string FIELD_ADM_TYPE = "ADM_TYPE";
        public const string FIELD_LEVELS = "LEVELS";
        public const string FIELD_PROVINCE_CODE = "PROVINCE_CODE";
        public const string FIELD_DISTRICT_CODE = "DISTRICT_CODE";
        public const string FIELD_COMMUNE_CODE = "COMMUNE_CODE";
        public const string FIELD_ADM_NAME = "ADM_NAME";
        public const string FIELD_PROVINCE_NAME = "PROVINCE_NAME";
        public const string FIELD_DISTRICT_NAME = "DISTRICT_NAME";
        public const string FIELD_COMMUNE_NAME = "COMMUNE_NAME";
        public const string FIELD_DESCRIPTIONS = "DESCRIPTIONS";
        public const string FIELD_DELETED = "DELETED";
        #endregion
        public const string FIELD_RECORD_COUNT = "RecordCount";

        private string _connectionString;

        public AdmUnitBO(string connString)
        {

            this._connectionString = connString;
        }
        public AdmUnitBO()
        {
            this._connectionString = Common.ConnectionUtil.TKTHDS_METADATA_CONNECTION_STRING;
        }

        public AdmUnitDAO _AdmUnitDAO;
        public AdmUnitDAO CreateSQLDAO()
        {
            return new AdmUnitDAO(_connectionString, DEFAULT_MAPPER_TYPE);
        }

        public DataPage<AdmUnitModel> GetDynamicListPaging(AdmUnitModel aModel, int pageSize, int pageIndex, bool compareEqual)
        {
            try
            {
                _AdmUnitDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
               {
                {AdmUnitModel.FIELD_ADM_UNIT_ID,FIELD_ADM_UNIT_ID},
                {AdmUnitModel.FIELD_CODE, FIELD_CODE},
                {AdmUnitModel.FIELD_P_CODE, FIELD_P_CODE},
                {AdmUnitModel.FIELD_ADM_NAME, FIELD_ADM_NAME},
                {AdmUnitModel.FIELD_ADM_TYPE, FIELD_ADM_TYPE},
                {AdmUnitModel.FIELD_COMMUNE_CODE, FIELD_COMMUNE_CODE},
                {AdmUnitModel.FIELD_COMMUNE_NAME, FIELD_COMMUNE_NAME},
                {AdmUnitModel.FIELD_DISTRICT_CODE, FIELD_DISTRICT_CODE},
                {AdmUnitModel.FIELD_DISTRICT_NAME, FIELD_DISTRICT_NAME},
                {AdmUnitModel.FIELD_LEVELS, FIELD_LEVELS},
                {AdmUnitModel.FIELD_PROVINCE_CODE, FIELD_PROVINCE_CODE},
                {AdmUnitModel.FIELD_PROVINCE_NAME, FIELD_PROVINCE_NAME},
                {AdmUnitModel.FIELD_DESCRIPTIONS, FIELD_DESCRIPTIONS},
                {AdmUnitModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT},
                {AdmUnitModel.FIELD_DELETED, FIELD_DELETED},
               };

                return _AdmUnitDAO.GetDynamicListPaging(aModel, pageSize, pageIndex, mapDictionary, compareEqual);

            }
            catch (Exception)
            {
                throw;
            }
        }
        public AdmUnitModel GetAdmUnitById(string id)
        {
            try
            {
                _AdmUnitDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>
               {
                    {AdmUnitModel.FIELD_ADM_UNIT_ID,FIELD_ADM_UNIT_ID},
                    {AdmUnitModel.FIELD_CODE, FIELD_CODE},
                    {AdmUnitModel.FIELD_P_CODE, FIELD_P_CODE},
                    {AdmUnitModel.FIELD_ADM_NAME, FIELD_ADM_NAME},
                    {AdmUnitModel.FIELD_ADM_TYPE, FIELD_ADM_TYPE},
                    {AdmUnitModel.FIELD_COMMUNE_CODE, FIELD_COMMUNE_CODE},
                    {AdmUnitModel.FIELD_COMMUNE_NAME, FIELD_COMMUNE_NAME},
                    {AdmUnitModel.FIELD_DISTRICT_CODE, FIELD_DISTRICT_CODE},
                    {AdmUnitModel.FIELD_DISTRICT_NAME, FIELD_DISTRICT_NAME},
                    {AdmUnitModel.FIELD_LEVELS, FIELD_LEVELS},
                    {AdmUnitModel.FIELD_PROVINCE_CODE, FIELD_PROVINCE_CODE},
                    {AdmUnitModel.FIELD_PROVINCE_NAME, FIELD_PROVINCE_NAME},
                    {AdmUnitModel.FIELD_DESCRIPTIONS, FIELD_DESCRIPTIONS},
                    {AdmUnitModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT},


               };
                return _AdmUnitDAO.GetById(id, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }

        }

        public bool createOrUpdateAdmUnit(AdmUnitModel model)
        {
            try
            {
                _AdmUnitDAO = CreateSQLDAO();
                return _AdmUnitDAO.createOrUpdateAdmUnit(model);
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool deleteAdmUnit(string[] id)
        {
            try
            {
                _AdmUnitDAO = CreateSQLDAO();
                return _AdmUnitDAO.deleteAdmUnit(id);
            }
            catch (Exception e)
            {
                return false;
            }
        }
        //lay ra ten tat ca cac huyen 
        public List<AdmUnitModel> GetAllDistrictName()
        {
            try
            {
                _AdmUnitDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {AdmUnitModel.FIELD_DISTRICT_CODE,FIELD_DISTRICT_CODE},
                    {AdmUnitModel.FIELD_DISTRICT_NAME,FIELD_DISTRICT_NAME}                    
                };
                return _AdmUnitDAO.GetAllDistrictName(mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }
        //lay ra ma huyen
        public List<AdmUnitModel> GetAllDistrict(string provinceid)
        {
            try
            {
                _AdmUnitDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {AdmUnitModel.FIELD_DISTRICT_CODE,FIELD_DISTRICT_CODE},
                    {AdmUnitModel.FIELD_DISTRICT_NAME,FIELD_DISTRICT_NAME}                    
                };
                return _AdmUnitDAO.GetAllDistrict(provinceid,mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }
        //lay ra ma tinh
        public List<AdmUnitModel> GetAllProvince()
        {
            try
            {
                _AdmUnitDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {AdmUnitModel.FIELD_PROVINCE_CODE,FIELD_PROVINCE_CODE},
                    {AdmUnitModel.FIELD_PROVINCE_NAME,FIELD_PROVINCE_NAME}                    
                };
                return _AdmUnitDAO.GetAllProvince(mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }
        //Lấy ra mã tỉnh để so sánh
        public List<AdmUnitModel>GetAllProvinceCode(string Province_Code)
        {
            try
            {
                _AdmUnitDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {AdmUnitModel.FIELD_PROVINCE_CODE, FIELD_PROVINCE_CODE}
                };
                return _AdmUnitDAO.GetAllProvinceCode(mapDictionary,Province_Code);
            }
            catch(Exception)
            {
                throw;
            }

        }
        public List<AdmUnitModel> GetAllAdmUnitByProvinceCode(string provinceCode)
        {
            try
            {
                _AdmUnitDAO = CreateSQLDAO();
                Dictionary<String, String> mapDictionary = new Dictionary<string, string>()
                {
                    {AdmUnitModel.FIELD_ADM_UNIT_ID,FIELD_ADM_UNIT_ID},
                    {AdmUnitModel.FIELD_CODE, FIELD_CODE},
                    {AdmUnitModel.FIELD_P_CODE, FIELD_P_CODE},
                    {AdmUnitModel.FIELD_ADM_NAME, FIELD_ADM_NAME},
                    {AdmUnitModel.FIELD_ADM_TYPE, FIELD_ADM_TYPE},
                    {AdmUnitModel.FIELD_COMMUNE_CODE, FIELD_COMMUNE_CODE},
                    {AdmUnitModel.FIELD_COMMUNE_NAME, FIELD_COMMUNE_NAME},
                    {AdmUnitModel.FIELD_DISTRICT_CODE, FIELD_DISTRICT_CODE},
                    {AdmUnitModel.FIELD_DISTRICT_NAME, FIELD_DISTRICT_NAME},
                    {AdmUnitModel.FIELD_LEVELS, FIELD_LEVELS},
                    {AdmUnitModel.FIELD_PROVINCE_CODE, FIELD_PROVINCE_CODE},
                    {AdmUnitModel.FIELD_PROVINCE_NAME, FIELD_PROVINCE_NAME},
                    {AdmUnitModel.FIELD_DESCRIPTIONS, FIELD_DESCRIPTIONS},
                    {AdmUnitModel.FIELD_RECORD_COUNT, FIELD_RECORD_COUNT},

                };
                return _AdmUnitDAO.GetAllAdmUnitByProvinceCode(provinceCode, mapDictionary);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public bool activeAdmUnit(string id, int request)
        {
            try
            {
                _AdmUnitDAO = CreateSQLDAO();
                return _AdmUnitDAO.activeAdmUnit(id, request);
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}
